package com.leyou.trade.util;

import com.leyou.auth.dto.UserDetails;
import com.leyou.auth.utils.UserContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author 虎哥
 */
@Component
public class CollectionNameBuilder {

    /**
     * 集合名称的前缀
     */
    @Value("${ly.mongo.collectionNamePrefix}")
    private String namePrefix;

    public String build(){
        // 获取用户id
        UserDetails user = UserContext.getUser();
        // 健壮性判断
        if (user == null) {
            // 用户未登录，无需生成集合名称
            return "";
        }
        // 用户id计算hash值，并计算集合名称
        int index = user.getId().hashCode() % 100;
        return namePrefix + index;
    }
}
